using System;
using System.Collections.Generic;
using System.Text;
using FlatRedBall;
using FlatRedBall.Input;
using FlatRedBall.AI.Pathfinding;
using FlatRedBall.Graphics.Animation;
using FlatRedBall.Graphics.Particle;

using FlatRedBall.Graphics.Model;
using FlatRedBall.Math.Geometry;
using FlatRedBall.Math.Splines;

using Cursor = FlatRedBall.Gui.Cursor;
using GuiManager = FlatRedBall.Gui.GuiManager;
using FlatRedBall.Localization;
using GraphicsAdapter = Microsoft.Xna.Framework.Graphics.GraphicsAdapter;

using Microsoft.Devices.Sensors;

#if FRB_XNA || SILVERLIGHT
using Keys = Microsoft.Xna.Framework.Input.Keys;
using Vector3 = Microsoft.Xna.Framework.Vector3;
using Texture2D = Microsoft.Xna.Framework.Graphics.Texture2D;
#endif

namespace NereusGame.Screens
{
	public partial class Options
	{
        private Sprite backGround;

        //options buttons
        Sprite soundSprite;
        Sprite calibrationSprite;
        Sprite titleSprite;
        AxisAlignedRectangle soundButton;
        AxisAlignedRectangle calibrationButton;
        bool isCalibrating;


        void CustomInitialize()
        {
            ResetCamera();
            InitializeBackground();
            InitializeButtons();

            isCalibrating = false;
        }

        void CustomActivity(bool firstTimeCalled)
        {
            if (InputManager.TouchScreen.ScreenPushed)
            {
                if (soundButton.IsPointInside(InputManager.TouchScreen.WorldXAt(0.0f), InputManager.TouchScreen.WorldYAt(0.0f)))
                {
                    if (GlobalContent.Sound)
                    {
                        SpriteManager.RemoveSprite(soundSprite);
                        soundSprite = SpriteManager.AddSprite(@"Content\Screens\Options\soundOff.png", "Global");
                        GlobalContent.Sound = false;
                        GlobalContent.MuteAudio();
                    }
                    else
                    {
                        SpriteManager.RemoveSprite(soundSprite);
                        soundSprite = SpriteManager.AddSprite(@"Content\Screens\Options\soundOn.png", "Global");
                        GlobalContent.Sound = true;
                        GlobalContent.UnMuteAudio();
                    }
                    soundSprite.ScaleY = SpriteManager.Camera.YEdge / 30;
                    soundSprite.ScaleX = 5f * soundSprite.ScaleY;
                    soundSprite.X = 0f;
                    soundSprite.Z = -1;
                }
                else if (calibrationButton.IsPointInside(InputManager.TouchScreen.WorldXAt(0.0f), InputManager.TouchScreen.WorldYAt(0.0f)))
                {
                    if (!isCalibrating)
                    {
                        SpriteManager.RemoveSprite(calibrationSprite);
                        calibrationSprite = SpriteManager.AddSprite(@"Content\Screens\Options\calibrating.png", "Global");
                        calibrationSprite.ScaleY = SpriteManager.Camera.YEdge / 20;
                        calibrationSprite.ScaleX = 5f * calibrationSprite.ScaleY;
                        calibrationSprite.X = 0f;
                        calibrationSprite.Y = -6;
                        calibrationSprite.Z = -1;
                        calibrationSprite.ColorOperation = FlatRedBall.Graphics.ColorOperation.ColorTextureAlpha;
                        calibrationSprite.Red = 1;
                        calibrationSprite.Green = calibrationSprite.Blue = 0;

                        isCalibrating = true;
                        Calibrate();
                    }
                    //else
                    //{
                    //    SpriteManager.RemoveSprite(calibrationSprite);
                    //    calibrationSprite = SpriteManager.AddSprite(@"Content\Screens\Options\calibrate.png", "Global");
                    //    isCalibrating = false;
                    //}
                    
                }

            }
        }

        // initialzie buttons for this screen
        private void InitializeButtons()
        {
            //options title
            titleSprite = SpriteManager.AddSprite(@"Content\Screens\Options\optionsTitle.png", "Global");
            titleSprite.ScaleY = SpriteManager.Camera.YEdge / 20;
            titleSprite.ScaleX = 5f * titleSprite.ScaleY;
            titleSprite.Y = 9f;
            titleSprite.X = 0f;
            titleSprite.Z = -1;


            //sound button
            if (GlobalContent.Sound)
            {
                soundSprite = SpriteManager.AddSprite(@"Content\Screens\Options\soundOn.png", "Global");
            }
            else
            {
                soundSprite = SpriteManager.AddSprite(@"Content\Screens\Options\soundOff.png", "Global");
            }
            soundSprite.ScaleY = SpriteManager.Camera.YEdge / 30;
            soundSprite.ScaleX = 5f * soundSprite.ScaleY;
            soundSprite.X = 0f;
            soundSprite.Z = -1;

            soundButton = ShapeManager.AddAxisAlignedRectangle();
            soundButton.ScaleY = SpriteManager.Camera.YEdge / 30;
            soundButton.ScaleX = 5f * soundSprite.ScaleY;
            soundButton.X = 0f;
            soundButton.Visible = false;


            //calibration button
            calibrationSprite = SpriteManager.AddSprite(@"Content\Screens\Options\calibrate.png", "Global");
            calibrationSprite.ScaleY = SpriteManager.Camera.YEdge / 20;
            calibrationSprite.ScaleX = 5f * calibrationSprite.ScaleY;
            calibrationSprite.X = 0f;
            calibrationSprite.Y = -6f;
            calibrationSprite.Z = -1;

            calibrationButton = ShapeManager.AddAxisAlignedRectangle();
            calibrationButton.ScaleY = SpriteManager.Camera.YEdge / 20;
            calibrationButton.ScaleX = 5f * calibrationSprite.ScaleY;
            calibrationButton.X = 0f;
            calibrationButton.Y = -6f;
            calibrationButton.Visible = false;

            


        }

        void CustomDestroy()
        {
            SpriteManager.RemoveSprite(backGround);
            SpriteManager.RemoveSprite(titleSprite);

            SpriteManager.RemoveSprite(soundSprite);
            SpriteManager.RemoveSprite(calibrationSprite);

            ShapeManager.Remove(soundButton);
            ShapeManager.Remove(calibrationButton);

        }


        static void CustomLoadStaticContent(string contentManagerName)
        {


        }

        private void ResetCamera()
        {
            // reset camera position
            SpriteManager.Camera.Position = SpriteManager.Camera.Velocity = SpriteManager.Camera.Acceleration = Vector3.Zero;
            SpriteManager.Camera.Position.Z = 40f;
        }

        // setup background for this screen
        private void InitializeBackground()
        {
            backGround = new Sprite();
            backGround.Texture = StarfieldOnly;
            SpriteManager.AddSprite(backGround);
            backGround.ScaleX = SpriteManager.Camera.XEdge / 2.35f;  // magic numbers are here to make the image fit the screen LOL
            backGround.ScaleY = SpriteManager.Camera.YEdge / 2.35f;  // magic numbers are here to make the image fit the screen LOL
            backGround.Z = -2;
        }

        private void Calibrate()
        {
            // Calibration data
            Microsoft.Devices.Sensors.Accelerometer accelerometer;
            Vector3 accelerometerState = Vector3.Zero;
            Vector3 accelerometerCalibrationData = Vector3.Zero;
            DateTime startTime;
            long samplesCount = 0;

            startTime = DateTime.Now;


            //Initialize the accelerometer
            accelerometer = new Microsoft.Devices.Sensors.Accelerometer();

            if (accelerometer.State == SensorState.Initializing || 
                accelerometer.State == SensorState.Ready)
            {
                accelerometer.ReadingChanged += (s, e) =>
                {
                    accelerometerState = 
                        new Vector3((float)e.X, (float)e.Y, (float)e.Z);

                    samplesCount++;
                    accelerometerCalibrationData += accelerometerState;

                    if (DateTime.Now >= startTime.AddSeconds(5))
                    {
                        accelerometer.Stop();

                        accelerometerCalibrationData.X /= samplesCount;
                        accelerometerCalibrationData.Y /= samplesCount;
                        accelerometerCalibrationData.Z /= samplesCount;


                        GlobalContent.AccelerometerCalibrationData = accelerometerCalibrationData;

                        isCalibrating = false;

                        //show on screen calibration done
                        SpriteManager.RemoveSprite(calibrationSprite);
                        calibrationSprite = SpriteManager.AddSprite(@"Content\Screens\Options\calibrate.png", "Global");
                        calibrationSprite.ScaleY = SpriteManager.Camera.YEdge / 20;
                        calibrationSprite.ScaleX = 5f * calibrationSprite.ScaleY;
                        calibrationSprite.X = 0f;
                        calibrationSprite.Y = -6;
                        calibrationSprite.Z = -1;
                    }
                };
            }
            accelerometer.Start();
            
            
        }
    }
}
